package com.wjerp.tplus.server.domain.service.impl;

import com.wjerp.tplus.server.api.request.FieldValueRequest;
import com.wjerp.tplus.server.api.response.FieldValueResponse;
import com.wjerp.tplus.server.common.constant.SystemInfoField;
import com.wjerp.tplus.server.common.security.JwtUser;
import com.wjerp.tplus.server.domain.entity.SystemInfo;
import com.wjerp.tplus.server.domain.mapper.SystemInfoMapper;
import com.wjerp.tplus.server.domain.service.SystemInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lishuailei
 */
@Service
public class SystemInfoServiceImpl implements SystemInfoService {

    @Resource
    private SystemInfoMapper systemInfoMapper;


    @Override
    public FieldValueResponse loadSysConfigInfoByCompanyCode(String companyCode) {
        List<SystemInfo> systemInfos = systemInfoMapper.selectByCompanyCode(companyCode);
        FieldValueResponse fieldValueResponse = new FieldValueResponse();
        systemInfos.stream().forEach(data->{
            String fieldValue = data.getFieldValue();
            boolean result = fieldValue.equals("1");
            if(data.getFieldName().equals(SystemInfoField.IS_CHECK_COMPANY_NAME)){
                fieldValueResponse.setCheckCompanyName(result);
            }else if(data.getFieldName().equals(SystemInfoField.CHECK_STATUS)){
                fieldValueResponse.setCheckInvoice(result);
            }
        });
        return fieldValueResponse;
    }

    @Override
    public void updateSystemConfig(FieldValueRequest request, String companyCode, String displayUserId) {
        //1.获取企业配置信息
        systemInfoMapper.deleteByCompanyCode(companyCode);
        //2. 公司系统配置信息 system_info
        SystemInfo systemInfo = new SystemInfo();
        systemInfo.setCompanyCode(companyCode);
        systemInfo.setFieldName(SystemInfoField.IS_CHECK_COMPANY_NAME);
        systemInfo.setFieldValue(request.getCheckCompanyName() ? "1" : "0");
        systemInfo.setUpdatedUser(displayUserId);
        systemInfo.setCreatedUser(displayUserId);
        systemInfoMapper.insertSelective(systemInfo);

        SystemInfo systemInfo2 = new SystemInfo();
        systemInfo2.setCompanyCode(companyCode);
        systemInfo2.setFieldName(SystemInfoField.CHECK_STATUS);
        systemInfo2.setFieldValue(request.getCheckInvoice() ? "1" : "0");
        systemInfo2.setUpdatedUser(displayUserId);
        systemInfo2.setCreatedUser(displayUserId);
        systemInfoMapper.insertSelective(systemInfo2);
    }
}
